from django.db import models

# Create your models here.
from django.db.models import CASCADE

from apps.book.models import AuthorInfo, CategoryInfo, PubHouse

"""
图书信息表
"""


class BookInfo(models.Model):
    book_name = models.CharField(max_length=255, verbose_name="书名", unique=False, null=False, blank=True)
    book_category = models.ForeignKey(
        to="CategoryInfo",
        to_field="category_name",
        on_delete=CASCADE,
        verbose_name="类型",
        db_constraint=False,
        null=True,
        blank=True,
    )
    book_author = models.ManyToManyField(
        to="AuthorInfo",
        verbose_name="作者",
        blank=True,
        db_constraint=False,
        related_name="author",
    )
    book_pub_house = models.ForeignKey(
        to="PubHouse",
        to_field="pub_name",
        on_delete=CASCADE,
        verbose_name="出版社",
        db_constraint=False,
        null=True,
        blank=True,
    )
    book_translator = models.ManyToManyField(
        to="AuthorInfo",
        verbose_name="译者",
        blank=True,
        db_constraint=False,
        related_name="translator"
    )
    book_des = models.TextField(verbose_name="描述", null=True, blank=True)
    book_version = models.IntegerField(verbose_name="版本")
    book_ISBN = models.CharField(verbose_name="ISBN", max_length=255, unique=True, )
    book_price = models.CharField(verbose_name="定价", max_length=255)
    book_cover = models.ImageField(
        verbose_name="封面",
        upload_to="cover/%Y/%m/%d/",
        null=True,
        blank=True,
        help_text="封面",
    )

    class Meta:
        db_table = "TAB_BOOK"
        verbose_name = "类型名称"

    def __str__(self):
        return f"{self.book_name}"
